package com.inspur.myapplication.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.inspur.myapplication.R;
import com.inspur.myapplication.dao.CartsDao;
import com.inspur.myapplication.entity.UserBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.HashMap;

public class CartsActivity extends AppCompatActivity {
    private ArrayList data;
    private TextView money;
    private boolean edit;
    private SimpleAdapter simpleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_carts);
        money=(TextView)findViewById(R.id.carts_sum);


        //实例化ImageLoader
        final ImageLoader imageLoader=ImageLoader.getInstance();
        //进行初始化
        ImageLoaderConfiguration imageLoaderConfiguration=new  ImageLoaderConfiguration.Builder(this).build();
        imageLoader.init(imageLoaderConfiguration);

        final CartsDao cartsDao=new CartsDao(this);
        data=cartsDao.queryCarts(UserBean.currentLoginUser);
        ListView listView=(ListView)findViewById(R.id.carts_lv);
        simpleAdapter=new SimpleAdapter(this,data,R.layout.layout_carts_row,new String[]{"goodsName","goodsPrice","goodsImg","quantity","goodsId","goodsId","goodsId"},new int[]{R.id.carts_row_goodsname,R.id.carts_row_price,R.id.carts_row_iv,R.id.carts_row_quantity,R.id.carts_row_chkbox,R.id.carts_row_add,R.id.carts_row_sub});
        simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object o, String s) {
                if(view.getId()==R.id.carts_row_iv){
                    imageLoader.displayImage(o.toString(),(ImageView) view);
                    return true;//如果返回true，代表ViewBinder来处理数据的显示
                }
                if(view.getId()==R.id.carts_row_chkbox){
                    //如果当前展示的组件是checkbox，此时传入的数据是goodsId，借助goodsId来确定当前用户选择的数据
                    final String goodsId=o.toString();
                    //判断是否处于选中状态，进行钩选
                    HashMap hashMap1=cartsDao.getOneCartDataById(goodsId,data);
                    if(hashMap1.get("sel").equals("1")){
                        ((CheckBox)view).setChecked(true);
                    }else {
                        ((CheckBox)view).setChecked(false);
                    }
                    ((CheckBox)view).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            //更新data中数据的状态
                            HashMap hashMap=cartsDao.getOneCartDataById(goodsId,data);
                            if(b){
                                hashMap.put("sel","1");
                            }else {
                                hashMap.put("sel","0");
                            }
                            float sum=cartsDao.calMoney(data);
                            money.setText(sum+"");
                        }
                    });
                    return true;
                }
                if(view.getId()==R.id.carts_row_add||view.getId()==R.id.carts_row_sub){
                    if(edit){
                        view.setVisibility(View.VISIBLE);
                    }else {
                        view.setVisibility(View.INVISIBLE);
                    }
                    final String goodsId=o.toString();
                    ((Button)view).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            HashMap oneData=cartsDao.getOneCartDataById(goodsId,data);
                            int quantity=Integer.parseInt(oneData.get("quantity").toString());
                            if(view.getId()==R.id.carts_row_add){
                                quantity++;
                            }else {
                                quantity--;
                            }
                            oneData.put("quantity",quantity+"");
                            float sum=cartsDao.calMoney(data);//计算总金额
                            money.setText(sum+"");
                            simpleAdapter.notifyDataSetChanged();
                        }
                    });
                    return true;
                }
                return false;
            }
        });

        listView.setAdapter(simpleAdapter);

        //处理全选按键
        ((CheckBox)findViewById(R.id.carts_all)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                cartsDao.checkAll(data,b);
                float sum=cartsDao.calMoney(data);
                money.setText(sum+"");
                simpleAdapter.notifyDataSetChanged();
            }
        });
    }

    public void editClick(View view){
        edit=!edit;
        if(edit){
            ((Button)view).setText("完成");
        }else {
            ((Button)view).setText("编辑");
        }
        //刷新列表
        simpleAdapter.notifyDataSetChanged();

    }
}
